package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.FocusImage;
import com.elinkway.infinitemovies.bean.RecommCartoon;
import com.elinkway.infinitemovies.bean.RecommMovie;
import com.elinkway.infinitemovies.bean.RecommTv;
import com.elinkway.infinitemovies.bean.RecommZongyi;
import com.elinkway.infinitemovies.bean.RecommendBase;
import com.elinkway.infinitemovies.bean.RecommendHomeData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecommendHomeParser extends LVideoMobileParser<RecommendHomeData> {
	private static final String SORT = "sort";
	private static final String NAME = "name";
	private static final String THEMEID = "themeid";
	private static final String PIC_M_HORSE = "pic_m_horse";
	private static final String VIDEOTYPE = "videotype";
	private static final String ALBUMID = "albumid";
	private static final String SUBNAME = "subname";
	private static final String PLAYURL = "playurl";
	private static final String PLAYSITE = "playsite";
	
	private static final String ZONGYI = "m_zone_zongyi";
	private static final String FOCUSES = "m_zone_one";
	private static final String MOVIE = "m_zone_movie";
	private static final String TV = "m_zone_tv";
	private static final String CARTOON = "m_zone_cartoon";
	
	private static final String DATA = "data";
	
	@Override
	public RecommendHomeData parse(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		RecommendHomeData homeData = new RecommendHomeData();
		if(data.has(FOCUSES)){
			homeData.setFocusImages(getChild(FocusImage.class, data.getJSONObject(FOCUSES)));
		}
		if(data.has(MOVIE)){
			homeData.setRecommMovies(getChild(RecommMovie.class,data.getJSONObject(MOVIE)));
		}
		if(data.has(TV)){
			homeData.setRecommTvs(getChild(RecommTv.class,data.getJSONObject(TV)));
		}
		if(data.has(ZONGYI)){
			homeData.setRecommZongyi(getChild(RecommZongyi.class,data.getJSONObject(ZONGYI)));
		}
		if(data.has(CARTOON)){
			homeData.setRecommCartoon(getChild(RecommCartoon.class,data.getJSONObject(CARTOON)));
		}
		return homeData;
	}
	private <T extends RecommendBase> List<T> getChild(Class<T> cls,JSONObject obj) throws JSONException, InstantiationException, IllegalAccessException{
		List<T> childs=new ArrayList<T>();
		if (obj.has(DATA)){
			JSONArray arr = obj.getJSONArray(DATA);
			for (int i=0;i<arr.length();i++){
				
				JSONObject childObj = arr.getJSONObject(i);
				T childData=cls.newInstance();
				
				if (childObj.has(SORT)){
//				    childData.setSort(childObj.getString(SORT));
				    }
				if (childObj.has(NAME)){
				    childData.setName(childObj.getString(NAME));
				    }
				if (childObj.has(THEMEID)){
				    childData.setThemeid(childObj.getString(THEMEID));
				    }
				if (childObj.has(PIC_M_HORSE)){
//				    childData.setPic_m_horse(childObj.getString(PIC_M_HORSE));
				    }
				if (childObj.has(VIDEOTYPE)){
				    childData.setVt(childObj.getString(VIDEOTYPE));
				    }
				if (childObj.has(ALBUMID)){
//				    childData.setAlbumid(childObj.getString(ALBUMID));
				    }
				if (childObj.has(SUBNAME)){
				    childData.setSubname(childObj.getString(SUBNAME));
				    }
				if (childObj.has(PLAYURL)){
				    childData.setPlayurl(childObj.getString(PLAYURL));
				    }
				if (childObj.has(PLAYSITE)){
//				    childData.setPlaysite(childObj.getString(PLAYSITE));
				    }
				childs.add(childData);
			}
		}
		
		return childs;
		
	}
}
